/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.entity.OutwardSys;

/**
 * description:  验证外部系统是否存在访问权限
 *
 * @author Hou Dayu 创建时间：2016-12-25
 */
public class CheckSysPlugin implements PluginConnector {

    protected IDao outwardSysDao;

    /**
     * TODO 验证系统名称和系统IP
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    @Override
    public Object execute(ParamsVo arg0) throws Exception {
        ResultEntity result = new ResultEntity();
        String sysEname = (String) arg0.getParams("sysEname");
        result.setResult("true");

        //不需要认证的系统
        String goOn = SystemTools.getInstance().getValue("otherGO");
        if (goOn.equalsIgnoreCase(sysEname)) {
            return true;
        }

        if (Validator.isNull(sysEname)) {
            result.setEntity("F");
            result.setMessage("系统名称不能为空");
            return result;
        }
        String sysAccreditip = (String) arg0.getParams("sysAccreditip");
        if (Validator.isNull(sysAccreditip)) {
            result.setEntity("F");
            result.setMessage("系统IP不能为空");
            return result;
        }

        //操作之前首先查看是否存在操作权限

        OutwardSys outwardSys = (OutwardSys) getVoBySysEname(sysEname);
        if (Validator.isEmpty(outwardSys)) {
            return false;
        }
        String ipindex = outwardSys.getSysAccreditip();

        if (!ipindex.contains(sysAccreditip)) {
            return false;
        }

        if ("1".equals(outwardSys.getSysStatus())) {
            result.setEntity("F");
            result.setMessage("当前系统已被冻结");
            return false;
        }
        return true;
    }

    /**
     * description:  根据系统名称查询系统访问权限
     *
     * @param sysEname
     * @return
     */
    public Object getVoBySysEname(String sysEname) {
        String sql = outwardSysDao.getSql(4);
        MethodParam param = new MethodParam("BySysEname", "", sql,
                "com.apache.passport.entity.OutwardSys");
        param.setParams("sysEname", sysEname);
        return outwardSysDao.selectSingle(param);
    }

    public void setOutwardSysDao(IDao outwardSysDao) {
        this.outwardSysDao = outwardSysDao;
    }
}
